app.factory(ꞌBoardServiceꞌ)   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
/*
2
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
3
 *
4
 * @author Julius Härtl <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *  
8
 *  This program is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU Affero General Public License as
10
 *  published by the Free Software Foundation, either version 3 of the
11
 *  License, or (at your option) any later version.
12
 *  
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU Affero General Public License for more details.
17
 *  
18
 *  You should have received a copy of the GNU Affero General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *  
21
 */
22
23
app.factory('BoardService', function(ApiService, $http, $q){
24
    var BoardService = function($http, ep, $q) {
25
        ApiService.call(this, $http, ep, $q);
26
    };
27
    BoardService.prototype = angular.copy(ApiService.prototype);
28
29
    BoardService.prototype.searchUsers = function(search) {
30
        var url = OC.generateUrl('/apps/deck/share/search/'+search);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
        var deferred = $q.defer();
32
        var self = this;
33
        $http.get(url).then(function (response) {
34
35
            self.sharees = [];
36
            // filter out everyone who is already in the share list
37
            angular.forEach(response.data, function(item) {
38
                var exists = false;
39
                angular.forEach(self.getCurrent().acl, function(acl) {
40
                    if (acl.participant === item.participant) {
41
                        exists = true;
42
                    }
43
                });
44
                if(!exists) {
45
                    self.sharees.push(item);
46
                }
47
            });
48
49
            deferred.resolve(response.data);
50
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
51
            deferred.reject('Error while update ' + self.endpoint);
52
        });
53
        return deferred.promise;
54
    };
55
56
    BoardService.prototype.addAcl = function(acl) {
57
        var board = this.getCurrent();
58
        var deferred = $q.defer();
59
        var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
60
        var _acl = acl;
61
        $http.post(this.baseUrl + '/' + acl.boardId + '/acl', _acl).then(function (response) {
62
            if(!board.acl) {
63
                board.acl = {};
64
            }
65
            board.acl[response.data.id] = response.data;
66
            deferred.resolve(response.data);
67
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
68
            deferred.reject('Error creating ACL ' + _acl);
69
        });
70
        acl = null;
71
        return deferred.promise;
72
    };
73
74
    BoardService.prototype.deleteAcl = function(acl) {
75
        var board = this.getCurrent();
76
        var deferred = $q.defer();
77
        var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
78
        $http.delete(this.baseUrl + '/' + acl.boardId + '/acl/' + acl.id).then(function (response) {
79
            delete board.acl[response.data.id];
80
            deferred.resolve(response.data);
81
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
82
            deferred.reject('Error deleting ACL ' + acl.id);
83
        });
84
        acl = null;
85
        return deferred.promise;
86
    };
87
88
    BoardService.prototype.updateAcl = function(acl) {
89
        var board = this.getCurrent();
90
        var deferred = $q.defer();
91
        var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
92
        var _acl = acl;
93
        $http.put(this.baseUrl + '/' + acl.boardId + '/acl', _acl).then(function (response) {
94
            board.acl[_acl.id] = response.data;
95
            deferred.resolve(response.data);
96
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
97
            deferred.reject('Error updating ACL ' + _acl);
98
        });
99
        acl = null;
100
        return deferred.promise;
101
    };
102
103
    BoardService.prototype.getPermissions = function() {
104
        var board = this.getCurrent();
105
        var deferred = $q.defer();
106
        $http.get(this.baseUrl + '/' + board.id + '/permissions').then(function (response) {
107
            board.permissions = response.data;
108
            console.log(board.permissions);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
109
            deferred.resolve(response.data);
110
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
111
            deferred.reject('Error fetching board permissions ' + board);
112
        });
113
    };
114
115
    BoardService.prototype.canRead = function() {
116
        if(!this.getCurrent() || !this.getCurrent().permissions) {
117
            return false;
118
        }
119
        return this.getCurrent().permissions['PERMISSION_READ'];
120
    }
121
122
    BoardService.prototype.canEdit = function() {
123
        if(!this.getCurrent() || !this.getCurrent().permissions) {
124
            return false;
125
        }
126
        return this.getCurrent().permissions['PERMISSION_EDIT'];
127
    }
128
129
    BoardService.prototype.canManage = function() {
130
        if(!this.getCurrent() || !this.getCurrent().permissions) {
131
            return false;
132
        }
133
        return this.getCurrent().permissions['PERMISSION_MANAGE'];
134
    }
135
136
    BoardService.prototype.canShare = function() {
137
        if(!this.getCurrent() || !this.getCurrent().permissions) {
138
            return false;
139
        }
140
        return this.getCurrent().permissions['PERMISSION_SHARE'];
141
    }
142
143
    service = new BoardService($http, 'boards', $q);
0 ignored issues
show
Bug introduced by
The variable service seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.service.
Loading history...
144
    return service;
145
    
146
});